using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 字符串转万，亿等单位
    /// </summary>
    public class FormatNumber
    {
        struct FormatNumberInfo
        {
            public long num;
            public string unit;
        }
        private static List<FormatNumberInfo> unitList = new List<FormatNumberInfo>()
    {
       new FormatNumberInfo {num = 10000,unit = "万"  },
       new FormatNumberInfo {num = 100000000,unit = "亿"  },
       new FormatNumberInfo {num = 100000000000,unit = "千亿"  },
       //new FormatNumberInfo {num = 1000,unit = "K"  },
       //new FormatNumberInfo {num = 1000000,unit = "M"  },
       //new FormatNumberInfo {num = 1000000000,unit = "B"  },
    };
        /// <summary>
        /// 数字格式化
        /// </summary>
        /// <param name="numTmp"></param>
        /// <param name="point_num">小数位数</param>
        /// <param name="isNeedFormat">是否需要千位加逗号</param>
        /// <returns></returns>
        public static string TransferUnit(long numTmp, int point_num, bool isNeedFormat = true)
        {
            for (int i = unitList.Count - 1; i >= 0; i--)
            {
                var t = unitList[i];
                var res = (double)numTmp / t.num;
                if (res >= 1)
                {
                    return Format(res, point_num, false, t.unit, isNeedFormat: isNeedFormat);
                }
            }

            return Format(numTmp, point_num, isNeedFormat: isNeedFormat);
        }
        public static string TransferUnit(double numTmp, int point_num, bool isNeedFormat = true)
        {
            for (int i = unitList.Count - 1; i >= 0; i--)
            {
                var t = unitList[i];
                var res = numTmp / t.num;
                if (res >= 1)
                {
                    return Format(res, point_num, false, t.unit, isNeedFormat: isNeedFormat);
                }
            }

            return Format(numTmp, point_num, isNeedFormat: isNeedFormat);
        }

        /// <summary>
        /// 数字千位加逗号格式化
        /// </summary>
        /// <param name="numTmp"></param>
        /// <param name="point_num">小数保留位数</param>
        /// <param name="point_zero_show">是否显示小数点后面的0，=true显示，=false不显示</param>
        /// <param name="unit">需要单位的可以填单位字符串</param>
        /// <returns></returns>
        public static string Format(double numTmp, int point_num = 2, bool point_zero_show = false, string unit = null, bool isNeedFormat = true)
        {
            string resultNum = "";
            //Debug.Log("double numTmp = " + numTmp);
            /*
            long inter = (long)numTmp;
            double point = numTmp - inter;
            */

            string[] tempStr = numTmp.ToString().Split(".");
            long inter = long.Parse(tempStr[0]);
            double point = 0;
            if (tempStr.Length >= 2)
            {
                point = double.Parse("0." + tempStr[1]);
            }

            //Debug.Log("double point = "+ point);
            string newStr = "";
            if (isNeedFormat && string.IsNullOrEmpty(unit))
            {
                newStr = NumberToMoneyFormat(inter);
            }
            else //有单位不需要逗号分割符
            {
                newStr = inter.ToString();
            }

            if (point > 0)
            {
                //string format = "{0:F"+ point_num+"}";
                //var strPoint = string.Format(format, point);

                double scale = Mathf.Pow(10f, point_num);
                //抹除多余位数
                int tempNum = (int)(point * scale);
                //Debug.Log("double tempNum = " + tempNum);
                var strPoint = ((tempNum / scale)).ToString();
                if (!point_zero_show)
                {
                    //转一次number可以去掉小数点后面的0
                    strPoint = double.Parse(strPoint).ToString();
                }
                resultNum = Utils.GetString(newStr, strPoint.Substring(1, strPoint.Length - 1));
            }
            else
            {
                resultNum = newStr;
            }

            if (!string.IsNullOrEmpty(unit))
            {
                resultNum = Utils.GetString(resultNum, unit);
            }
            return resultNum;
        }
        /// <summary>
        /// 数字每三位加逗号
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string NumberToMoneyFormat(long num)
        {
            string sign = "";
            if (num < 0) sign = "-";

            string result = "";
            string sNum = num.ToString();
            int len = sNum.Length;
            while (len > 3)
            {
                string tmpNum = sNum.Substring(len - 3);
                result = Utils.FormatString(",{0}{1}", tmpNum, result);
                len = len - 3;
                sNum = sNum.Substring(0, len);
            }
            if (len > 0)
            {
                result = Utils.FormatString("{0}{1}", sNum, result);
            }

            return Utils.GetString(sign, result);
        }

        public static string FormatNumberNoCommaWithUnit(long numTmp, int point_num)
        {
            return "";
        }


    }
}